package com.mikepenz.interactor.imp;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.mikepenz.bean.RequestBean;
import com.mikepenz.interactor.SendOrderInteractor;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.MethodCode;
import com.mikepenz.utils.MethodType;
import com.quickdv.helper.NetWorkImp;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lilongfei on 15/9/21.
 */
public class SendOrderInteractorImp extends NetWorkImp implements SendOrderInteractor{
    RequestListener<JSONObject> listener;
    public SendOrderInteractorImp(Context c,RequestListener<JSONObject> listener) {
        super(c);
        this.listener = listener;
    }

    @Override
    protected void onRequestSuccess(Object tag, JSONObject result) {

        if (result == null){
            onRequestExcption(null);
            return;
        }
        JSONObject response = result.getJSONObject(MethodCode.responseContent_key);
        if (response == null) {
            onRequestExcption(null);
        } else {
            int  statue = response.getInteger(MethodCode.responseStatue);

            if (statue == MethodCode.NetState.SUCCESS.getValue()){

                    JSONObject obj = response.getJSONObject(MethodCode.OBJECT);
                    listener.Success(tag + "", obj);

            }else //TODO 未完成
                    listener.Error(tag + "", "订单提交失败，请尝试重新提交");
        }
    }

    @Override
    protected void onRequestExcption(VolleyError error) {
        listener.Exception("",error);
    }

    /**
     *
     * @param userId
     * @param cardId
     * @param carId
     * @param carType
     * @param orderType
     * @param sendType 租用类型
     * @param currentTime
     * @param startTime
     * @param endTime
     */
    @Override
    public void sendOrder(String userId, String cardId, String carId, String carType, int orderType, int sendType, String currentTime, String startTime, String endTime) {
        RequestBean bean = new RequestBean();
        Map<String,Object> params = new HashMap<>();
        params.put("carId",carId);
        params.put("tenancyType",sendType);
        params.put("sessionId",userId);
        params.put("cardId",cardId);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        bean.setRequestParams(params);
        bean.setRequestCode(MethodType.RENT);
        addFastJsonQueue(Request.Method.POST, MethodType.RENT, (JSONObject) JSON.toJSON(bean));
        startRequest();
    }
}
